import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router, RouterLink, RouterModule } from '@angular/router';
import { ApiService } from '../../../../providers/ApiPro.service';
import { NzModalService } from 'ng-zorro-antd';

@Component({
  selector: 'app-ResOilcardInfo',
  templateUrl: './ResOilcardInfo.component.html',
  styleUrls: ['./ResOilcardInfo.component.scss']
})
export class ResOilcardInfoComponent implements OnInit {

  public cardDetail:any =[];
  public data = [];
  public pageIndex:number = 1;
  public _total = 0;
  public chargeMoney:number;
  public oilCarId:number;
  public carId:number;
  public carList:any =[];
  public driverId:number;
  public driverList:any =[];

  constructor(
    public apiService: ApiService,
    private route: ActivatedRoute,
    private router: Router,
    private confirmServ: NzModalService
  ) { }

  ngOnInit() {
    console.log(this.pageIndex);
    this.getId();
    this.getCarList();
    this.getDriverList();
  }

  getId(){
    this.route.params.subscribe((params) => {
      this.oilCarId = params['id'];
            this.getCardCharge();
       });
 }

 getCarList(){
  let url = "car/findcarlist";
   this.apiService.get(url,{})
     .then(res =>{
       this.carList = res.result;
       console.log(this.carList);
     })
 }

 carIdChange(carId){
   console.log(carId);
 }

 getDriverList(){
  let url = "findalldrivers";
   this.apiService.get(url,{})
     .then(res =>{
       this.driverList = res.result;
       console.log(this.driverList);
     })
 }

 driverIdChange(driverId){
   console.log(driverId);
 }

 getCardCharge(){
  console.log(this.oilCarId);
  console.log(this.pageIndex);
  let url = "findoilcardscharge";
   this.apiService.get(url,{"oilCardId" : this.oilCarId , "pageIndex" : this.pageIndex , "pageSize" : 10 })
     .then(res =>{
       this._total = res.totalCount;
       this.data = res.result;
       console.log(this.data);
     })
}

cardCharge(){
  console.log(this.chargeMoney);
  let oilCard = {
    "carId" : this.carId,
    "chargeMoney" : this.chargeMoney,
    "driverId" : this.driverId,
    "oilCardId" : this.oilCarId,
  }
console.log(oilCard);
let url = "oilcardcharge";
this.apiService.post(url,oilCard)
  .then(res =>{
    console.log(res);
    if(res.result){
      this.success();          
    }else{
      this.error();
    }
  })
}

resetIndex(e) {
  this.pageIndex = e;
  this.ngOnInit();
}
 
error() {
  this.confirmServ.error({
    title: '错误',
    content: '输入的信息有误或已生成信息',
    okText:'返回'
  });
}

success(){
  let self = this;
    this.confirmServ.success({
    title: '成功',
    content: '即将返回油卡管理页面',
    okText:'返回',
    onOk() {
      self.router.navigateByUrl("layout/res/oil");
    },
  });
}
}
